package edu.wust.homework.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.wust.homework.entity.Student;
import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    //自定义根据UserName查询
    @Select("SELECT * FROM student WHERE user_name = #{userName}")
    Student getStuByUserName(@Param("userName") String userName);

    @Insert("INSERT INTO student (user_name, password, name, sex, age, phone) " +
            "VALUES (#{userName}, #{password}, #{name}, #{sex}, #{age}, #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertStudent(Student student);

    @Update("UPDATE student " +
            "SET password = #{newPassword}" +
            "WHERE user_name = #{userName} AND password = #{oldPassword}")
    int modifyPassword(@Param("userName")String userName,
                       @Param("oldPassword") String oldPassword,
                       @Param("newPassword")String newPassword);


    @Delete("DELETE FROM student WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);


    @Delete("DELETE FROM student WHERE user_name =#{userName}")
    int deleteByUsername(@Param("userName")String userName);


    @Select("SELECT * FROM student")
    List<Student> getAllStudentsByPage(Page<Student> page);

    @Select("SELECT * FROM student")
    List<Student> getAllStudents();

    @Update("UPDATE student SET name=#{name}, age=#{age}, sex=#{sex} ,phone =#{phone} WHERE user_name=#{userName}")
    int updateStudentInfo(Student student);

}
